package com.lmscn.lms.api.util;

import com.lmscn.lms.api.model.*;

/**
 * Created by Czl on 2015/7/25.
 */
public class CommonUtil {

	public static String mediaUrl;
	public static String bannerUrl;
	public static String imgUrl;
	public static String uploadShareUrl;
	public static String shareUrl;
	public static String testInfo;
	public static String examInfo;

	/** 极光推送消息 **/
	public static String appKey;
	public static String appSecret;

	public static String getBannerUrl(BannerImages bannerImages) {
		// String bannerUrl1 = bannerUrl + "banner/" +
		// bannerImages.getStr("img_url");
		String bannerUrl1 = shareUrl + "index/" + bannerImages.getStr("img_url");
		return bannerUrl1;
	}

	/**
	 * 获取推送的图片
	 * 
	 * @date 2016年11月24日15:55:35
	 * @param push
	 * @return
	 */
	public static String getPushimgUrl(Push push) {
		String pushimgUrl = shareUrl + "push/" + push.getStr("img");
		return pushimgUrl;
	}
	/**
	 * 获取闪屏页图片地址
	 * 
	 * @param push
	 * @return
	 */
	public static String getWelcomeimgUrl(WelcomPages w) {
		String pushimgUrl = shareUrl + "welcome/" + w.getStr("img");
		return pushimgUrl;
	}
	/**
	 * 分类板块图片获取
	 * 
	 * @param w
	 * @return
	 */
	public static String getCategoryColumimgUrl(CategoryColum w) {
		String pushimgUrl = shareUrl + "category/" + w.getStr("img");
		return pushimgUrl;
	}
	
	/**
	 * 獲取咨詢頭圖
	 * 
	 * @param img
	 * @return
	 */
	public static String getArticleHeadUrl(String img) {
		String pushimgUrl = shareUrl + "articlehead/" + img;
		return pushimgUrl;
	}
	
	/**
	 * 获取头像路径
	 * 
	 * 调整为阿里云OSS路径 
	 * 更换为共享路径
	 * 
	 * @date 2016年11月22日11:12:46
	 * @date 2016年11月24日16:01:45
	 */
	public static String getHeadImgUrl(Student student) {
		String headImg = shareUrl + "head_imgs/" +  student.getStr("little_img");
		return headImg;
	}

	/**
	 * 获取视频路径
	 * 
	 * @param courseBase
	 * @return
	 */
	public static String getMediaUrl(CourseBase courseBase) {
		CourseLesson courseLesson = courseBase.getCourseLesson();
		String mediaUrls = null;
//		if (courseLesson.getStr("assemble_type").equals("1")) {
//			mediaUrls = mediaUrl + courseBase.getStr("url");
//		} else if (courseLesson.getStr("assemble_type").equals("3") || courseLesson.getStr("assemble_type").equals("4")) {
//			mediaUrls = mediaUrl + courseBase.getStr("course_no") + "/index.html";
//		}
		if (courseLesson != null) {
			if (("4").equals(courseLesson.getStr("assemble_type"))) {
				mediaUrls = mediaUrl +"gyfmt/app/"+ courseBase.getStr("course_no") + "/index.html";
			} else {
				mediaUrls = mediaUrl + "gywk/" + courseBase.getStr("url");
			}
		} else {
			mediaUrls = mediaUrl + "gywk/" + courseBase.getStr("url");
		}
		
		return mediaUrls;
	}

	/**
	 * 获取课程小图片路径
	 * 
	 * @param courseBase
	 * @return
	 */
	public static String getsmallImgUrl(CourseBase courseBase) {
		String imgUrl = mediaUrl + "gywk/logo/" +  courseBase.getStr("small_img");
		return imgUrl;
	}

	/**
	 * 获取课程大图片路径
	 * 
	 * @param courseBase
	 * @return
	 */
	public static String getbigImgUrl(CourseBase courseBase) {
		String imgUrl = mediaUrl + "gywk/logo/" +  courseBase.getStr("large_img");
		return imgUrl;
	}

	

	/**
	 * 获取随机头像路径
	 */
	public static String getRandomHeadImgUrl(int num) {
		String headImg = imgUrl + "touxiang/" + num + ".jpeg";
		return headImg;
	}

	/**
	 * 获取班级图片路径
	 * 
	 * @param clazz
	 * @return
	 */
	public static String getClazzImgUrl(Clazz clazz) {
		String clazzImg = imgUrl + "clazz/" + clazz.getStr("img_path");
		return clazzImg;
	}

	/**
	 * 获取擂台图片路径
	 * 
	 * @param leiTai
	 * @return
	 */
	public static String getLeiImgUrl(LeiTai leiTai) {
		String leiImg = imgUrl + "leitai/" + leiTai.getStr("img");
		return leiImg;
	}

	/**
	 * 获取我的考试图片路径
	 * 
	 * @return
	 */
	public static String getMyExamImgUrl(MyExam myExam) {
		String examImg = imgUrl + "myExam/" + myExam.getStr("img_path");
		return examImg;
	}

	/**
	 * 获取组织
	 * 
	 * @param student
	 * @return
	 */
	public static String getPhbPost(Student student) {
		StringBuilder sb = new StringBuilder("");
		if (student.get("org_id") != null) {
			Org org = Org.dao.findById(student.getBigInteger("org_id"));
			if (org == null) {
				return "";
			}
			// 省
			if (org.getStr("org_no").length() == 6) {
				Org province = Org.dao.getByOrgNo(org.getStr("org_no").substring(0, 6));
				String name = province.getStr("name");
				if (name != null && name.length() > 1 && name.startsWith("团")) {
					name = name.substring(1, name.length());
				}
				if (name != null && name.length() > 1 && name.endsWith("委")) {
					name = name.substring(0, name.length() - 1);
				}
				sb.append(name);
			}
			// 市
			if (org.getStr("org_no").length() >= 9) {
				Org city = Org.dao.getByOrgNo(org.getStr("org_no").substring(0, 9));
				String name = city.getStr("name");
				if (name != null && name.length() > 1 && name.startsWith("团")) {
					name = name.substring(1, name.length());
				}
				if (name != null && name.length() > 1 && name.endsWith("委")) {
					name = name.substring(0, name.length() - 1);
				}
				sb.append(name);
			}

			// 县
			if (org.getStr("org_no").length() >= 12) {
				Org county = Org.dao.getByOrgNo(org.getStr("org_no").substring(0, org.getStr("org_no").length()));
				String name = county.getStr("name");
				if (name != null && name.length() > 1 && name.startsWith("团")) {
					name = name.substring(1, name.length());
				}
				if (name != null && name.length() > 1 && name.endsWith("委")) {
					name = name.substring(0, name.length() - 1);
				}
				sb.append(name);
			}
		}

		sb.append("");
		return sb.toString();
	}

	/**
	 * string 转Long
	 * 
	 * @param str
	 * @return
	 */
	public static Long[] splitToLongArray(String str) {
		return splitToLongArray(str, ",");

	}

	public static String getUpdateUrl() {
		String updateUrl = imgUrl + "123.txt";
		return updateUrl;
	}

	public static Long[] splitToLongArray(String str, String regex) {
		if (str == null || str.trim().length() == 0) {
			return null;
		}
		Long[] longs;
		try {
			String[] arr = str.split(regex);
			longs = new Long[arr.length];
			int count = 0;
			for (int i = 0; i < arr.length; i++) {
				if (arr[i].trim().length() != 0) {
					longs[count] = Long.parseLong(arr[i]);
					count++;
				}
			}
			return longs;
		} catch (NumberFormatException e) {
			// e.printStackTrace();
			return null;
		}
	}

}
